/**
 * 
 */
package uk.ac.ebi.velvet.util;

import java.io.File;

/**
 * @author mhaimel
 * @author $Author: mhaimel $
 * @version $Revision: 1.1 $
 */
public class PathUtil {
	
	public static void main(String[] args) {
		Class<?> clazz = PathUtil.class;

		System.out.println(getJavaPath(clazz));
		System.out.println(getJavaClassPath(clazz));
		System.out.println(getPath(clazz, "sql.xml"));
		
	}
	
	public static String getJavaPath(Class<?> clazz){
		return getPath(clazz.getPackage(),clazz.getSimpleName()+".java");
	}
	
	public static String getJavaClassPath(Class<?> clazz){
		return getPath(clazz.getPackage(),clazz.getSimpleName()+".class");
	}
	
	/**
	 * Uses the {@link Class#getPackage()} information 
	 * for {@link #getPath(Package, String)}
	 * @param clazz
	 * @param fileName
	 * @return String representation of a pat
	 */
	public static String getPath(Class<?> clazz, String fileName){
		return getPath(clazz.getPackage(),fileName);
	}
	
	/**
	 * @see {@link #getPath(Class, String)}
	 * @param Object 
	 * @param fileName
	 * @return String representation of a pat
	 */
	public static String getPath(Object obj, String fileName){
		return getPath(obj.getClass(),fileName);
	}
	
	public File getFile(Object obj,String fileName){
		return new File(obj.getClass().getResource(getPath(obj, fileName)).getFile());
	}
	
	/**
	 * Changes a package name e.g. uk.ac.ebi.proteome to a path name
	 * /uk/ac/ebi/proteome with NO / at the end
	 * 
	 * @param packge
	 * @return String representation of a path
	 */
	public static String getPath(Package packge){
		String pName = packge.getName();
		pName = '/'+pName.replace('.', '/');
		return pName;
	}
	
	/**
	 * Returns to path to a file in the resource:
	 * e.g. sql.xml file in uk.ac.ebi.proteome -> /uk/ac/ebi/proteome/sql.xml
	 * @param packge
	 * @param fileName
	 * @return String representation of the path
	 */
	public static String getPath(Package packge, String fileName){
		String pName = packge.getName();
		pName = getPath(packge).concat("/").concat(fileName);
		return pName;
	}

}
